package com.xjrsoft.module.customerTwo.EMP.electricMeter.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.DateUtil;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.dto.TElectricMeterDto;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.dto.SaveTElectricMeterFormDataDto;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.vo.TElectricMeterCostVo;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.vo.TElectricMeterVo;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.vo.TElectricMeterListVo;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.dto.TElectricMeterListDto;
import com.xjrsoft.module.customerTwo.EMP.price_setting.service.ITPriceSettingService;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_day.vo.GetWaterTotalVO;
import com.xjrsoft.module.customerTwo.EMP.t_water_cost_records_month.service.ITWaterCostRecordsMonthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.entity.TElectricMeter;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.vo.TElectricMeterVo;
import com.xjrsoft.module.customerTwo.EMP.electricMeter.service.ITElectricMeterService;

/**
 *  控制器
 *
 * @author dongxinhang
 * @since 2023-07-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/electricmeter")
@Api(value = "ceshi", tags = "ceshi接口")
public class TElectricMeterController {
	//电费单价设置
	private final ITPriceSettingService itPriceSettingService;

	private final ITElectricMeterService tElectricMeterService;

	private final ITWaterCostRecordsMonthService tWaterCostRecordsMonthService;


	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入tElectricMeter")
	@SaCheckPermission(value = {"ElectricMeter:view", "ElectricMeter:modify"}, mode = SaMode.OR)
	public Response<TElectricMeterVo> getFormData(@PathVariable String id) {
		// 主表数据
		TElectricMeter tElectricMeter = tElectricMeterService.getById(id);
		TElectricMeterVo tElectricMeterVo = BeanUtil.copy(tElectricMeter, TElectricMeterVo.class);
		return Response.ok(tElectricMeterVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入tElectricMeter")
		public Response<PageOutput<TElectricMeterListVo>> getPageList(TElectricMeterListDto listDto) {
		IPage<TElectricMeter> page = tElectricMeterService.getPageList(listDto);
		List<TElectricMeterListVo> records = BeanUtil.copyList(page.getRecords(), TElectricMeterListVo.class);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}



	/**
	 * 退宿结算获取费用
	 */
	@GetMapping("/settlement")
	@ApiOperation(value = "退宿结算获取费用", notes = "传入退宿参数")
	public Response settlement(TElectricMeterDto dto) {
		String start = dto.getStart();
		String end = dto.getEnd();
		if(end.isEmpty()){
			return Response.notOk("结算日期为空");
		}
		TElectricMeterCostVo electricCost = new TElectricMeterCostVo();
		electricCost.setCheckout_date(dto.getEnd());
		//获取电费单价
		BigDecimal electricityPrice = itPriceSettingService.getElectricityPrice();
		//处理时间范围
		dto.setStart(dto.getStart().replace("-","")+"000000");
		dto.setEnd(dto.getEnd().replace("-","")+"235959");
		//获取用电量
		BigDecimal number = tElectricMeterService.getElectricCost(dto);
		if(null == number){
			number = new BigDecimal("0");
		}
		List<TElectricMeterCostVo> list = new ArrayList<>();
        electricCost.setIteams("电费");
		electricCost.setPrices(electricityPrice);
		electricCost.setNumbers(number);
		electricCost.setAmounts(electricityPrice.multiply(number));
		list.add(electricCost);
		//======================================计算水费==================================
		end = getNextDate(end);
		dto.setStart(start);
		dto.setEnd(end);
		//获取水费单价
		BigDecimal coldWaterPrice = itPriceSettingService.getColdWaterPrice();
		BigDecimal heatWaterPrice = itPriceSettingService.getHeatWaterPrice();
		//删除历史数据
		tWaterCostRecordsMonthService.deleteWaterDate(start,end);
		tWaterCostRecordsMonthService.deleteWaterCostRecordsDay(start,end);
		//数据写入中转表(原始数据变成根据房间号、日期汇总每天最小用水量(当天最小-昨天最小=昨天整天用水量))
		int count = tWaterCostRecordsMonthService.addTransitionData(dto.getStart(),dto.getEnd());
		if(count != 0){
			//中转表数据汇总（从中转表最小用水量计算当天用水量）
			tWaterCostRecordsMonthService.addSumData(dto.getStart(),dto.getEnd());
		}
		//取水费汇总
		GetWaterTotalVO vo = tElectricMeterService.getWaterTotal(dto);
		if(vo == null){
			TElectricMeterCostVo clodWaterCost = new TElectricMeterCostVo();
			clodWaterCost.setIteams("冷水费");
			clodWaterCost.setPrices(coldWaterPrice);
			clodWaterCost.setNumbers(new BigDecimal("0"));
			clodWaterCost.setAmounts(new BigDecimal("0.00"));
			clodWaterCost.setCheckout_date(dto.getEnd());
			clodWaterCost.setNotes("水表异常");
			TElectricMeterCostVo heatWaterCost = new TElectricMeterCostVo();
			heatWaterCost.setIteams("热水费");
			heatWaterCost.setPrices(heatWaterPrice);
			heatWaterCost.setNumbers(new BigDecimal("0"));
			heatWaterCost.setAmounts(new BigDecimal("0.00"));
			heatWaterCost.setCheckout_date(dto.getEnd());
			heatWaterCost.setNotes("水表异常");
			list.add(clodWaterCost);
			list.add(heatWaterCost);
			return Response.ok(list);
		}
		TElectricMeterCostVo clodWaterCost = new TElectricMeterCostVo();
		clodWaterCost.setIteams("冷水费");
		clodWaterCost.setPrices(coldWaterPrice);
		clodWaterCost.setNumbers(vo.getColdWaterTotal());
		clodWaterCost.setAmounts(vo.getColdWaterTotal().divide(new BigDecimal("1000")).multiply(coldWaterPrice).setScale(4, BigDecimal.ROUND_HALF_UP));
		clodWaterCost.setCheckout_date(dto.getEnd());
		list.add(clodWaterCost);
		TElectricMeterCostVo heatWaterCost = new TElectricMeterCostVo();
		heatWaterCost.setIteams("热水费");
		heatWaterCost.setPrices(heatWaterPrice);
		heatWaterCost.setNumbers(vo.getHeatWaterTotal());
		heatWaterCost.setAmounts(vo.getHeatWaterTotal().divide(new BigDecimal("1000")).multiply(heatWaterPrice).setScale(4, BigDecimal.ROUND_HALF_UP));
		heatWaterCost.setCheckout_date(dto.getEnd());
		list.add(heatWaterCost);
		return Response.ok(list);
	}


	private String getNextDate(String date){
		Date date1 = DateUtil.parse(date,DateUtil.PATTERN_DATE);
		Date date2 = DateUtil.plusDays(date1,1);
		return DateUtil.format(date2,DateUtil.PATTERN_DATE);
	}

}
